<?php


namespace app\api\controller\v1;


use app\common\controller\UserBaseController;
use app\common\library\IDMan;
use app\api\model\NoticeModel;
use app\api\model\ServiceAreaModel;

class NoticeController extends UserBaseController
{
    function addAction()
    {
        $this->paramlist = [
            'token' => 'string',
            'city' => 'optstring',
            'name' => 'string',
            'type' => 'int',
            'img' => 'string',
            'url' => 'string',
            'remark' => 'string',
            'start_time' => 'datetime',
            'expire_time' => 'datetime',
        ];

        $this->check_params();
        $this->check_token();

        $info = [
            'city_id' => empty($this->argv['city']) ? null : $this->argv['city'],
            'name' => $this->argv['name'],
            'type' => $this->argv['type'],
            'img' => $this->argv['img'],
            'url' => $this->argv['url'],
            'remark' => $this->argv['remark'],
            'start_time' => $this->argv['start_time'],
            'expire_time' => $this->argv['expire_time'],
            'id' => IDMan::random_string_with_date(),
            'state' => 0,
        ];

        $info = (new NoticeModel())->add($info);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }


    function updateAction()
    {
        $this->paramlist = [
            'token' => 'string',
            'id' => 'string',
            'city' => 'optstring',
            'name' => 'optstring',
            'type' => 'optint',
            'img' => 'optstring',
            'url' => 'optstring',
            'remark' => 'optstring',
            'start_time' => 'optdatetime',
            'expire_time' => 'optdatetime',
            'state' => 'optint',
        ];

        $this->check_params();
        $this->check_token();

        $data = [

        ];
        if (isset($this->argv['city'])) {
            if (empty($this->argv['city'])) {
                $data['city_id'] = null;
            } else {
                $data['city_id'] = $this->argv['city'];
            }
        }

        if (isset($this->argv['name'])) {
            $data['name'] = $this->argv['name'];
        }
        if (isset($this->argv['type'])) {
            $data['type'] = $this->argv['type'];
        }
        if (isset($this->argv['img'])) {
            $data['img'] = $this->argv['img'];
        }
        if (isset($this->argv['url'])) {
            $data['url'] = $this->argv['url'];
        }
        if (isset($this->argv['remark'])) {
            $data['remark'] = $this->argv['remark'];
        }
        if (isset($this->argv['start_time'])) {
            $data['start_time'] = $this->argv['start_time'];
        }
        if (isset($this->argv['expire_time'])) {
            $data['expire_time'] = $this->argv['expire_time'];
        }

        if (isset($this->argv['state'])) {
            $data['state'] = $this->argv['state'];
        }

        (new NoticeModel())->update($this->argv['id'], $data);

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'coordinate' => ['optarray', null],
            'type' => 'optint',
            'pno' => 'optint',
            'psize' => 'optint',
            'city' => 'optstring',
        );

        $this->check_params();

        $city_id = null;
        if (isset($this->argv['city']) && !empty($this->argv['city'])) {
            $city_id = $this->argv['city'];
        } else if (isset($this->argv['coordinate'])) {
            $svcas = (new ServiceAreaModel())->query_list_near($this->argv['coordinate']);
            if (!empty($svcas)) {
                $city_id = $svcas[0]['city_id'];
            }
        }

        $cd = [];
        if (!empty($city_id)) {
            $cd['city_id'] = $city_id;
        }

        if (isset($this->argv['type']) && $this->argv['type'] > 0) {
            $cd['type'] = $this->argv['type'];
        }

        $l = (new NoticeModel())->query_list($cd);

        $this->resp['list'] = $l;
        $this->resp['total'] = count($l);
        $this->resp['is_skip'] = 0;

        return $this->ret_resp();
    }
}